﻿using System;
using System.Data;
using Ntt.NttData.Been.CommonLibrary.Database.SqlServer;

namespace NTTDATA.BEENChina.CaseMonitor.ServerChecker
{
    [Serializable]
    public class Relay:Server
    {
        protected override bool CheckRunnint()
        {
            //throw new NotImplementedException();
            //本次 收到的受番数量 发送到受番数量
            receiveOrder_current = GetReceiveOrder();
            sendOrder_current = GetSendOrder();

            if (receiveOrder_current.Equals(receiveOrder_previous) &&
                sendOrder_current.Equals(sendOrder_previous))
            {
                SaveCurrentResult();
                return false;
            }
            else
            {
                SaveCurrentResult();
                return true;
            }

         
        }

        private void SaveCurrentResult()
        {
            //记录本次结果
            receiveOrder_previous = receiveOrder_current;
            sendOrder_previous = sendOrder_current;
        }

        private int receiveOrder_previous, sendOrder_previous;
        private int receiveOrder_current, sendOrder_current;


        private String GetDBconnString()
        {
            Log.Console.Info(Address);
            return String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", this.Address, DBName, UserId, PassWord);
        }

        private String ReceiveOrderSql()
        {
            return "SELECT COUNT(*)  FROM t_OrderData WITH(NOLOCK)"; 
 
        }

        private String SendOrderSql()
        {
            return "SELECT COUNT(*)  FROM t_OrderData WITH(NOLOCK) WHERE SendTime IS NOT NULL";
             
        }

        private  int GetReceiveOrder()
        {
            int count = 0;
            try
            { 
           
                int.TryParse(new SqlHelper().ExecuteScalar(GetDBconnString(), CommandType.Text, ReceiveOrderSql()).ToString(), out count);
                
                Log.Console.Debug(string.Format("Relay Address:{1}{0}  ReceiveOrder:{2}{0}  Sql{3}",Environment.NewLine,Address,count,ReceiveOrderSql()));
                

            }
            catch (Exception ex)
            {
                Log.Console.Error(string.Format("Relay Address:{1}{0}  ReceiveOrder:{2}{0}  Sql{3}", Environment.NewLine, Address, count, ReceiveOrderSql()));
                Log.Console.Error("CaseChecker ServerChecker Relay GetReceiveOrder", ex);
            }
           
            return count;
        }


        private int GetSendOrder()
        {

            int count = 0;
             try
            { 
                int.TryParse(new SqlHelper().ExecuteScalar(GetDBconnString(), CommandType.Text, SendOrderSql()).ToString(), out count);

                Log.Console.Debug(string.Format("Relay Address:{1}{0}  SendOrder:{2}{0}  Sql{3}", Environment.NewLine, Address, count, SendOrderSql()));
               


            }
             catch (Exception ex)
             {
                 Log.Console.Error(string.Format("Relay Address:{1}{0}  SendOrder:{2}{0}  Sql{3}", Environment.NewLine, Address, count, SendOrderSql()));
                 Log.Console.Error("CaseChecker ServerChecker Relay GetSendOrder", ex);
             }
            return count;
        }
    }
}
